﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using T.ApiResults;
using T.Settlement.API.Application.Command.ExpensesCommand;

namespace T.Settlement.API.Controllers
{
    /// <summary>
    /// 应付/收费控制器(写)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class ExpensesController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="mediator"></param>
        public ExpensesController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加应付/收费
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Create(ExpensesAddCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new ExpensesAddCommand
                {
                    ExpensesId = command.ExpensesId,
                    TicketNumber = command.TicketNumber,
                    ShipperIsUnit = command.ShipperIsUnit,
                    OutsourcedUnits = command.OutsourcedUnits,
                    PaymentMethod = command.PaymentMethod,
                    OtherPaymentMethod = command.OtherPaymentMethod,
                    Tonnage = command.Tonnage,
                    CostManagementPrice = command.CostManagementPrice,
                    CostManagementAmount = command.CostManagementAmount,
                    BusinessDate = command.BusinessDate,
                    CostManagementAttn = command.CostManagementAttn,
                    CostManagementRemark = command.CostManagementRemark,
                    CheckStatus = command.CheckStatus,
                    CheckPeople = command.CheckPeople,
                    CheckDate = command.CheckDate,
                    ExpensesClassify = command.ExpensesClassify,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    CreatedBy= HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                    CreatedDate=DateTime.Now
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除应付/收费
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Delete(ExpensesDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new ExpensesDelCommand
                {
                    ExpensesId = command.ExpensesId,
                    TicketNumber = command.TicketNumber,
                    ShipperIsUnit = command.ShipperIsUnit,
                    OutsourcedUnits = command.OutsourcedUnits,
                    PaymentMethod = command.PaymentMethod,
                    OtherPaymentMethod = command.OtherPaymentMethod,
                    Tonnage = command.Tonnage,
                    CostManagementPrice = command.CostManagementPrice,
                    CostManagementAmount = command.CostManagementAmount,
                    BusinessDate = command.BusinessDate,
                    CostManagementAttn = command.CostManagementAttn,
                    CostManagementRemark = command.CostManagementRemark,
                    CheckStatus = command.CheckStatus,
                    CheckPeople = command.CheckPeople,
                    CheckDate = command.CheckDate,
                    ExpensesClassify = command.ExpensesClassify,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    DeletedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                    DeletedDate=DateTime.Now
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改应付/收费
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Update(ExpensesUpdCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new ExpensesUpdCommand
                {
                    ExpensesId = command.ExpensesId,
                    TicketNumber = command.TicketNumber,
                    ShipperIsUnit = command.ShipperIsUnit,
                    OutsourcedUnits = command.OutsourcedUnits,
                    PaymentMethod = command.PaymentMethod,
                    OtherPaymentMethod = command.OtherPaymentMethod,
                    Tonnage = command.Tonnage,
                    CostManagementPrice = command.CostManagementPrice,
                    CostManagementAmount = command.CostManagementAmount,
                    BusinessDate = command.BusinessDate,
                    CostManagementAttn = command.CostManagementAttn,
                    CostManagementRemark = command.CostManagementRemark,
                    CheckStatus = command.CheckStatus,
                    CheckPeople = command.CheckPeople,
                    CheckDate = command.CheckDate,
                    ExpensesClassify = command.ExpensesClassify,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                    UpdatedDate=DateTime.Now
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 核对应付/收费
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Check(ExpensesCheckCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new ExpensesCheckCommand
                {
                    ExpensesId = command.ExpensesId,
                    TicketNumber = command.TicketNumber,
                    ShipperIsUnit = command.ShipperIsUnit,
                    OutsourcedUnits = command.OutsourcedUnits,
                    PaymentMethod = command.PaymentMethod,
                    OtherPaymentMethod = command.OtherPaymentMethod,
                    Tonnage = command.Tonnage,
                    CostManagementPrice = command.CostManagementPrice,
                    CostManagementAmount = command.CostManagementAmount,
                    BusinessDate = command.BusinessDate,
                    CostManagementAttn = command.CostManagementAttn,
                    CostManagementRemark = command.CostManagementRemark,
                    CheckStatus = command.CheckStatus,
                    CheckPeople = command.CheckPeople,
                    CheckDate = command.CheckDate,
                    ExpensesClassify = command.ExpensesClassify,
                    CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value,//获取公司Id,
                    User_Id = int.Parse(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value), //获取用户Id,
                    UpdatedBy = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value,//获取用户名称
                    UpdatedDate=DateTime.Now
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批删应付/收费
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> BatchDel([FromQuery]ExpensesBatchDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(new ExpensesBatchDelCommand
                {
                    ExpensesId = command.ExpensesId,
                });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
